/**
 * Created by Administrator on 2018/6/1 0001.
 */
const Article = require('../model/article.model')
const config = require('../config')
const {handleError,handleSuccess} = require("../units/handle")
const crypto = require('crypto')
//密码md5加密
const md5Decode = (pwd)=>{
    return crypto.createHash("md5").update(pwd).digest('hex')
}

class ArticleController {
    // 获取文章列表
    static async getArts (ctx) {
        const {current_page = 1,
            page_size = 10,
            keyword = '',
            state = 1,
            publish = 1,
            tag,
            type,
            date,
            hot } = ctx.query

        // 过滤条件
        const options = {
            sort: { create_at: -1 },
            page: Number(current_page),
            limit: Number(page_size),
            populate: ['tag'],
            select: '-content'
        }


        // 参数
        const querys = {}

        // 关键词查询
        if (keyword) {
            const keywordReg = new RegExp(keyword)
            querys['$or'] = [
                { 'title': keywordReg },
                { 'content': keywordReg },
                { 'description': keywordReg }
            ]
        }

        // 按照state查询
        if (['1', '2'].includes(state)) {
            querys.state = state
        }

        // 按照公开程度查询
        if (['1', '2'].includes(publish)) {
            querys.publish = publish
        }

        // 按照类型程度查询
        if (['1', '2', '3'].includes(type)) {
            querys.type = type
        }

        // 按热度排行
        if (hot) {
            options.sort = {
                'meta.views': -1,
                'meta.likes': -1,
                'meta.comments': -1
            }
        }

        // 时间查询
        if (date) {
            const getDate = new Date(date)
            if(!Object.is(getDate.toString(), 'Invalid Date')) {
                querys.create_at = {
                    "$gte": new Date((getDate / 1000 - 60 * 60 * 8) * 1000),
                    "$lt": new Date((getDate / 1000 + 60 * 60 * 16) * 1000)
                }
            }
        }


        if (tag) querys.tag = tag

        // // 如果是前台请求，则重置公开状态和发布状态
        // if (!authIsVerified(ctx.request)) {
        //     querys.state = 1
        //     querys.publish = 1
        // }

        // 查询
        const result = await Article
            .find()
            .catch(err =>{
                console.log(err)
                ctx.throw(500, '服务器内部错误')
            })
        console.log(result)
        // if (result) {
        //     handleSuccess({
        //         ctx,
        //         result: {
        //             pagination: {
        //                 total: result.total,
        //                 current_page: result.page,
        //                 total_page: result.pages,
        //                 page_size: result.limit
        //             },
        //             list: result.docs
        //         },
        //         message: '列表数据获取成功!'
        //     })
        // } else handleError({ ctx, message: '获取列表数据失败'})
    }
}
module.exports = ArticleController